const minimist = require("minimist")
const path = require("path")
const { build } = require("esbuild")
const args = minimist(process.argv.slice(2))

const target = args._[0] || "reactivity"
const format = args.f || "global"

const pkg = require(path.resolve(__dirname, `../packages/${target}/package.json`))
const outputFormat = format === "global" ? "iife" : format === "cjs" ? "cjs" : "esm"

const outfile = path.resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`)

build({
  entryPoints: [path.resolve(__dirname, `../packages/${target}/src/index.ts`)],
  outfile,
  bundle: true,
  sourcemap: true,
  format: outputFormat,
  globalName: pkg.buildOptions.name,
  platform: format === "cjs" ? "node" : "browser",
  watch: {
    onRebuild(error) {
      if (!error) {
        console.log("rebuild success")
      }
    }
  }
}).then(() => {
  console.log("watching-----")
})